<template>

  <div class="app-container" style="font-size:12px">
    <div class="filter-container">
       <el-form ref="dataForm"  label-position="left" label-width="120px" style="width: 100%;">
      <el-select v-model="listQuery.city" placeholder="选择城市" filterable class="filter-item" style="width:130px;margin-top:7px" @change="getRegionList">
        <el-option label="请选择城市"  value="" />
        <el-option
          v-for="item in city"
          :key="item.city"
          :label="item.name"
          :value="item.city"
        />
      </el-select>
      <el-select v-model="listQuery.merchant" placeholder="选择商户" filterable class="filter-item" style="width:130px;margin-top:6px">
        <el-option label="请选择商户" value="" />
        <el-option
          v-for="item in merchant_list"
          :key="item.merchant_id"
          :label="item.merchant_name"
          :value="item.merchant_id"
        />
      </el-select>
      <el-select v-model="listQuery.sale_id" placeholder="选择销售" filterable class="filter-item" style="width:130px;margin-top:6px">
        <el-option label="请选择销售" value="" />
        <el-option
          v-for="item in sale_list"
          :key="item.sale_id"
          :label="item.real_name"
          :value="item.sale_id"
        />
      </el-select>

      <el-select v-model="listQuery.b_model" placeholder="放款模式" class="filter-item" style="width:130px;margin-top:6px">
        <el-option label="放款模式" value="" />
        <el-option label="正常" value="1" />
        <el-option label="买断" value="2" />
        <el-option label="绿通" value="3" />
      </el-select>
      <el-select v-model="listQuery.order_status" placeholder="订单状态" class="filter-item" style="width:130px;margin-top:6px">
        <el-option label="订单状态" value="" />
        <el-option label="初审" value="1" />
        <el-option label="进件" value="2" />
        <el-option label="通过" value="3" />
      </el-select> 
      <el-date-picker v-model="listQuery.date1" style="width:150px;" format="yyyy-MM-dd" type="datetime" placeholder="开始日期" />
      <el-date-picker v-model="listQuery.date2" style="width:150px" format="yyyy-MM-dd" type="datetime" placeholder="结束日期" />
      <el-input v-model="listQuery.customer" placeholder="客户姓名\备注关键词" style="width: 180px;margin-top:6px" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:6px" @click="handleFilter">查询</el-button>
      <!-- <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:6px" @click="excelout">导出</el-button> -->
      </el-form>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" size="mini"  border  fit highlight-current-row show-summary :summary-method="getSummaries" style="width: 100%;">
      <el-table-column label="日期" align="center" width="90px" >
        <template slot-scope="{row}">
          <!-- {{ row.create_time|formatDate}} -->
          {{ row.create_time }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="城市" align="center">
        <template slot-scope="{row}">
          <span>{{ row.city }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="医院" align="center" width="70px">
        <template slot-scope="{row}">
          <span>{{ row.logo_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" width="80px">
        <template slot-scope="{row}">
          <span >{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证" align="center" width="145px">
        <template slot-scope="{row}">
          <span>{{ row.idcard }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" width="40px">
        <template slot-scope="{row}">
          <span>{{ row.sex }}</span>
        </template>
      </el-table-column>
      <el-table-column label="布尔/备注" align="center" width="110px" >
        <template slot-scope="{row}">
          <span v-if="row.bool_score>=80||row.dangyu!=''||row.guanyu!=''||row.guansi!=''" style="color:red">{{ row.bool_score }} {{row.dangyu}} {{row.guanyu}} {{row.guansi}}</span>
          <span v-else>{{ row.bool_score }}{{row.dangyu}}{{row.guanyu}}{{row.guansi}}</span>
        </template>
      </el-table-column>
      <el-table-column label="百 融 | 自然人" align="center" width="70px" >
        <template slot-scope="{row}">
          <span v-if="row.bairong_result=='未命中'">{{row.bairong_result}}</span>
          <span v-else style="color:red">{{row.bairong_result}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="同盾分数" align="center" width="70px">
        <template slot-scope="{row}">
          <span>{{ row.tong_score }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="放款模式" align="center">
        <template slot-scope="{row}">
          <span>{{ row.model_name }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="是否进件" align="center" width="50px">
        <template slot-scope="{row}">
          <span v-if="row.order_status>3">是</span>
          <span v-if="row.order_status<4">否</span>
        </template>
      </el-table-column>
      <el-table-column label="是否通过" align="center" width="50px">
        <template slot-scope="{row}">
          <span v-if="row.order_status>6">是</span>
          <span v-if="row.order_status<7">否</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" width="60px">
        <template slot-scope="{row}">
          <span>{{ row.total_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="月还款" align="center" width="70px">
        <template slot-scope="{row}">
          <span>{{ row.everymonth }}</span>
        </template>
      </el-table-column>
  
      <el-table-column label="打款比例" align="center" width="50px">
        <template slot-scope="{row}">
          <span v-if="row.factoring_rate!=''">{{ row.factoring_rate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打款金额" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.first_pay }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签单员" align="center" width="60px">
        <template slot-scope="{row}">
          <span>{{ row.sale_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核员" align="center" width="95px">
        <template slot-scope="{row}">
          <span>{{ row.sname }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否手术" align="center" width="70px">
        <template slot-scope="{row}">
          <span v-if="row.operation_time!=null">是</span>
          <span v-if="row.operation_time==null">否</span>
        </template>
      </el-table-column> -->
      <el-table-column label="是否术后照" align="center" width="60px">
        <template slot-scope="{row}">
          <span></span>
        </template>
      </el-table-column>
      <el-table-column label="手术时间" align="center" width="90px">
        <template slot-scope="{row}">
          <span>{{ row.operation_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打款时间" align="center" width="90px">
        <template slot-scope="{row}">
          <span>{{ row.first_paid_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" width="300">
        <template slot-scope="{row}">
          <span>{{ row.desc }}</span>
        </template>
      </el-table-column>
     
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row,$index}" v-if="row.is_paid!=1">
          <el-dropdown :hide-on-click="false" >
            <span class="el-dropdown-link" style="color:#67C23A">
              更多操作
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="description(row)">备注信息</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="success" v-if="row.order_status>6" @click="operation(row.order_id)">手术时间</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="info" @click="detail(row.order_id)">订单详情</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogOperationVisible" width="600px">
      <el-form   label-position="left" label-width="120px" style="width: 300px; margin-left:70px;">
        <el-form-item label="手术时间" prop="real_name">
          <el-date-picker v-model="operationdata.date" style="width:150px;" format="yyyy-MM-dd" type="datetime" placeholder="手术日期" />
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogOperationVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="operationSubmit()">
           确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="备注" :visible.sync="returnOrderAble" style="width:1000px;margin:auto">
      <el-form ref="dataForm" :model="form" label-width="120px">
        <el-form-item label="备注">
          <el-input v-model="form.desc" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="descriptionSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
  
</template>

<script>
import {
getRegion, excelout, importList,descriptionSubmit
} from '@/api/finance'
import { formatDate } from '@/utils/formatDate.js'
import { merchantListOnlyMerchant } from '@/api/merchant'
import { fetchList } from '@/api/sale'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  filters: {
   	formatDate(time) {
   	        time = time * 1000
   	        const date = new Date(time)

   	        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
   	      }
  },
  data() {
    return {
      form: {
        desc: '',
        refund_time: '',
        order_id: '',
        serverMoneyNun:''
      },
      sums:['合计'],
      operationdata:{
          date:'',
          order_id:''
        },
      dialogOperationVisible:false,
      returnOrderAble: false,
      tableKey: 0,
      returnOrderAble: false,
      serverAble:false,
    
      tableRecord: 1,
      list: null,
      allcount: [],
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      city: [],
      quyu: [],
      listQuery: {
        page: 1,
        limit: 15,
        customer: '',
        rebackdate: '',
        city: '',
        quyu: '',
        merchant: '',
        sale_id: '',
        b_model: '',
        order_status: '',
        excel: 0,
        date1:'',
        date2:'',
      },
      listRecordQuery: {
        page: 1,
        limit: 20,
        order_id: ''
      },
      listMerchantQuery: {
        page: 1,
        limit: 500
      },
      textMap: {
        record: '订单记录',
        create: 'Create'
      },
      merchant_list: [],
      sale_list: [],
      dialogRecordVisible: false,
      dialogStatus: '',
      downloadLoading: false,
    }
  },
  created() {
    this.getList()
    this.getRegionList()
    this.getMerchantList()
    this.getSaleList()
  },
  methods: {
    getList() {
      importList(this.listQuery).then(response => {
        if (!this.listQuery.excel) {
          this.list = response.data.list.list;
          this.total = response.data.list.count;
          this.allcount = response.data.countall;
          this.sums[13] = response.data.sum;
          setTimeout(() => {
            this.listLoading = false
          }, 0.1 * 1000)
        } else {
          this.listQuery.excel = 0
        }
      })
    },
    getSummaries(param) {
      return this.sums;
    },
    detail(order_id) {
      this.$router.push('/order/detail/' + order_id)
    },
    description(row) {
        this.returnOrderAble = true;
        this.form.order_id = row.order_id;
        this.form.desc = row.desc;
    },
    descriptionSubmit() {
      descriptionSubmit(this.form).then(response => {
        this.$notify({
          title: '提示',
          message: '备注成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
        this.returnOrderAble = false
      })
    },
    handleFilter() {
      this.listLoading = true
      this.listQuery.page = 1
      this.getList()
    },
    excelout() {
      this.listLoading = true
      this.listQuery.page = 1
      this.listQuery.limit = this.total
      excelout(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },

    getRegionList() {
      getRegion(this.listQuery).then(response => {
        if (this.listQuery.city) {
          this.quyu = response.data
        } else {
          this.city = response.data
        }
      })
    },
    getSaleList() {
      fetchList().then(response => {
        this.sale_list = response.data.list
      })
    },
    getMerchantList() {
      merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
        this.merchant_list = response.data.list
      })
    },
    tableRowClassName({row, rowIndex}) {
        if (row.bool_score >= 80) {
          return 'red';
        } 
        return '';
    },
    operation(order_id) {
      this.dialogOperationVisible = true
      this.operationdata.order_id = order_id
    },
    operationSubmit() {
        if(this.operationdata.date==''){
          this.$notify({
            title: '提示',
            message: '请完善信息',
            type: 'warning',
            duration:2000
          });
          return;
        }
        this.dialogOperationVisible = false;
        this.listLoading = true;
        operationSubmit(this.operationdata).then(response => {
            this.listLoading = false
          if(response.code==200){
            this.$notify({  
              title: '提示',
              message: '保存成功',
              type: 'success',
              duration:2000
            });
          }else{
              this.$notify({
              title: '提示',
              message: '网络错误',
              type: 'warning',
              duration:2000
            });
          }
        })
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  td{padding: 0;}
  select{
    width:100px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
   .el-table .warning-row {
      background: orangered;
    }
  .red{background: orangered;}
  .el-table .success-row {
    background: #98dd72;
  }
  .el-table .default-row {
    background: #ccc;
  }
</style>
